package com.yanfan.parking.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import javax.validation.Valid;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yanfan.common.utils.DateUtils;
import com.yanfan.common.utils.StringUtils;
import com.yanfan.parking.constant.ParkingConstants;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yanfan.common.annotation.Log;
import com.yanfan.common.core.controller.BaseController;
import com.yanfan.common.core.domain.AjaxResult;
import com.yanfan.common.enums.BusinessType;
import com.yanfan.parking.domain.IbmsParkingFlowHour;
import com.yanfan.parking.service.IIbmsParkingFlowHourService;
import com.yanfan.common.utils.poi.ExcelUtil;
import com.yanfan.common.core.page.TableDataInfo;

/**
 * 车流量每小时统计Controller
 *
 * @author kerwincui
 * @date 2024-08-14
 */
@Api(tags = "车流量每小时统计")
@RestController
@RequestMapping("/parking/flow")
public class IbmsParkingFlowHourController extends BaseController {
    @Autowired
    private IIbmsParkingFlowHourService ibmsFlowHourService;

    /**
     * 查询车流量每小时统计列表
     */
    @ApiOperation("车流量每小时统计分页列表")
    @PreAuthorize("@ss.hasPermi('parking:flow:list')")
    @GetMapping("/list")
    public TableDataInfo list(IbmsParkingFlowHour ibmsParkingFlowHour) {
        startPage();
        List<IbmsParkingFlowHour> list = ibmsFlowHourService.selectIbmsParkingFlowHourList(ibmsParkingFlowHour);
        return getDataTable(list);
    }

    /**
     * 导出车流量每小时统计列表
     */
    @ApiOperation("导出车流量每小时统计列表")
    @PreAuthorize("@ss.hasPermi('parking:flow:export')")
    @Log(title = "车流量每小时统计", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, IbmsParkingFlowHour ibmsParkingFlowHour) {
        List<IbmsParkingFlowHour> list = ibmsFlowHourService.selectIbmsParkingFlowHourList(ibmsParkingFlowHour);
        ExcelUtil<IbmsParkingFlowHour> util = new ExcelUtil<IbmsParkingFlowHour>(IbmsParkingFlowHour.class);
        util.exportExcel(response, list, "车流量每小时统计数据");
    }

    /**
     * 获取车流量每小时统计详细信息
     */
    @ApiOperation("获取车流量每小时统计详细信息")
    @PreAuthorize("@ss.hasPermi('parking:flow:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(ibmsFlowHourService.getById(id));
    }

    /**
     * 新增车流量每小时统计
     */
    @ApiOperation("新增车流量每小时统计")
    @PreAuthorize("@ss.hasPermi('parking:flow:add')")
    @Log(title = "车流量每小时统计", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody IbmsParkingFlowHour ibmsParkingFlowHour) {
        return toAjax(ibmsFlowHourService.save(ibmsParkingFlowHour));
    }

    /**
     * 修改车流量每小时统计
     */
    @ApiOperation("修改车流量每小时统计")
    @PreAuthorize("@ss.hasPermi('parking:flow:edit')")
    @Log(title = "车流量每小时统计", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody IbmsParkingFlowHour ibmsParkingFlowHour) {
        return toAjax(ibmsFlowHourService.updateById(ibmsParkingFlowHour));
    }

    /**
     * 删除车流量每小时统计
     */
    @ApiOperation("删除车流量每小时统计")
    @PreAuthorize("@ss.hasPermi('parking:flow:remove')")
    @Log(title = "车流量每小时统计", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(ibmsFlowHourService.removeBatchByIds(Arrays.asList(ids)));
    }

    /**
     * 小时车流量统计图数据
     * @return
     */
    @ApiOperation("小时车流量统计图数据")
    @GetMapping("/hourChart")
    public AjaxResult hourChart(){
        JSONObject resultData = new JSONObject();
        //进出场车流量数据
        List<IbmsParkingFlowHour> inOutData = ibmsFlowHourService.list(new LambdaQueryWrapper<IbmsParkingFlowHour>()
                .likeRight(IbmsParkingFlowHour::getInOutTime, DateUtils.getDate()));
        if (StringUtils.isNotEmpty(inOutData)){
            //进场流量
            List<Integer> inFlows = inOutData.stream().map(IbmsParkingFlowHour::getInVehicleFlow).collect(Collectors.toList());
            //出场流量
            List<Integer> outFlows = inOutData.stream().map(IbmsParkingFlowHour::getOutVehicleFlow).collect(Collectors.toList());
            //时间集合
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:00");
            List<String> dates = inOutData.stream().map(ibmsParkingFlowHour -> formatter.format(ibmsParkingFlowHour.getInOutTime())).collect(Collectors.toList());
            resultData.put("inList",inFlows);
            resultData.put("outList",outFlows);
            resultData.put("date",dates);
        }else {
            resultData.put("inList",new ArrayList<>());
            resultData.put("outList",new ArrayList<>());
            resultData.put("date",new ArrayList<>());
        }

        return success(resultData);
    }
}
